package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatResource;
import com.happy.cat.project.system.service.ICatResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 文件列表Controller
 *
 * @author verne
 * @date 2020-07-26
 */
@AllArgsConstructor
@RestController
@RequestMapping("/oss/catResource" )
@Api("文件列表 API")
public class CatResourceController extends BaseController {

    private final ICatResourceService iCatResourceService;

    /**
     * 查询文件列表列表
     */
    @ApiOperation(value = "查询文件列表列表",notes = "查询文件列表列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('oss:catResource:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatResource catResource)
    {
        startPage();
        LambdaQueryWrapper<CatResource> lqw = new LambdaQueryWrapper<CatResource>();
        if (StringUtils.isNotBlank(catResource.getResValue())){
            lqw.eq(CatResource::getResValue ,catResource.getResValue());
        }
        if (StringUtils.isNotBlank(catResource.getResType())){
            lqw.eq(CatResource::getResType ,catResource.getResType());
        }
        if (StringUtils.isNotBlank(catResource.getVideoId())){
            lqw.eq(CatResource::getVideoId ,catResource.getVideoId());
        }
        if (StringUtils.isNotBlank(catResource.getRemarks())){
            lqw.eq(CatResource::getRemarks ,catResource.getRemarks());
        }
        List<CatResource> list = iCatResourceService.list(lqw);
        return getDataTable(list);
    }
    /**
     * 获取文件列表详细信息
     */
    @ApiOperation(value = "获取文件列表详细信息",notes = "获取文件列表详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('oss:catResource:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatResourceService.getById(id));
    }

    /**
     * 新增文件列表
     */
    @ApiOperation(value = "新增文件列表",notes = "新增文件列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('oss:catResource:add')" )
    @Log(title = "文件列表" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatResource catResource) {
        return toAjax(iCatResourceService.save(catResource) ? 1 : 0);
    }

    /**
     * 修改文件列表
     */
    @ApiOperation(value = "修改文件列表",notes = "修改文件列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('oss:catResource:edit')" )
    @Log(title = "文件列表" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatResource catResource) {
        return toAjax(iCatResourceService.updateById(catResource) ? 1 : 0);
    }

    /**
     * 删除文件列表
     */
    @ApiOperation(value = "删除文件列表",notes = "删除文件列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('oss:catResource:remove')" )
    @Log(title = "文件列表" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatResourceService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
